# -*- coding: UTF-8 -*-

from apscheduler.schedulers.background import BackgroundScheduler
from flask import Flask
from flask_apscheduler import APScheduler  # 引入APScheduler
import json
from zsupgrade import Upgrader


class Config(object):  # 创建配置，用类
    f = open("jobs.json", encoding='utf-8')
    JOBS = json.load(f)
    f.close()


def updatejob():
    """
    一个函数，用来做定时更新
    :return:
    """
    print("execute upgrader script start.")
    upgrader = Upgrader()
    if upgrader.execute():
        print("Upgrade success")
    else:
        print("Upgrade failed")

    print("execute upgrader script end.")


app = Flask(__name__)  # 实例化flask
app.config.from_object(Config())  # 为实例化的flask引入配置


@app.route('/pingpong')
def pingpong():
    """
    首页视图函数，就返回个hello
    :return:
    """
    return 'pingpong'


if __name__ == '__main__':
    schedulerBack = BackgroundScheduler({'apscheduler.job_defaults.max_instances': "16"})
    scheduler = APScheduler(scheduler=schedulerBack)  # 实例化APScheduler
    scheduler.init_app(app)  # 把任务列表放进flask
    scheduler.start()  # 启动任务列表
    app.run()  # 启动flask
